package com.lwm.aop.annotation.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author liweimin
 * @date 2021/6/11 10:43
 * @since
 */
@Component("logger")
@Aspect
public class Logger {

    @Pointcut("execution(* com.lwm.aop.annotation.service.impl.*.*(..))")
    private void pt1() {
    }


    @Before("pt1()")
    public void printLogBefore() {
        System.out.println("Logger类中的前置通知。。。");
    }

    @AfterReturning("pt1()")
    public void printLogAfter() {
        System.out.println("Logger类中的后置通知。。。");
    }

    @AfterThrowing("pt1()")
    public void exceptionLog() {
        System.out.println("Logger类中的异常通知。。。");
    }

    @After("pt1()")
    public void finalLog() {
        System.out.println("Logger类中的最终通知。。。");
    }


    //    @Around("pt1()")
    public Object aroundLog(ProceedingJoinPoint pjp) {
        Object returnValue = null;
        try {
            Object[] args = pjp.getArgs();//得到方法执行的参数
            System.out.println("Logger类中的环绕通知========前置通知");
            returnValue = pjp.proceed(args);//明确调用切入点方法
            System.out.println("Logger类中的环绕通知========后置通知");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("Logger类中的环绕通知========异常通知");
        } finally {
            System.out.println("Logger类中的环绕通知========最终通知");
        }

        return returnValue;
    }
}
